package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;

import modle.*;
import util.*;
import view.Bill.MyTableModel;
import dao.*;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.Toolkit;

public class Ship_Orders extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField textField;
	private JButton btnBack;
	private JButton btnShip;
	private JLabel ioto_Label;
	private Object[][] rowData = new Object[1000][3];
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	ResourceBundle rd;
	
	/**
     * 表头（列名）
     */
    private Object[] columnNames = {"订单的编号", "菜品是否已发货", "用户的编号"};
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Ship_Orders frame = new Ship_Orders();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Ship_Orders() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Ship_Orders.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 547, 590);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setBounds(187, 13, 154, 120);
		lblNewLabel.setIcon(new ImageIcon(Ship_Orders.class.getResource("/img/orders1.jpg")));
		contentPane.add(lblNewLabel);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 146, 501, 248);
		contentPane.add(scrollPane);
		
		table = new JTable(new MyTableModel());
		table.setRowHeight(30);										//设置table行高
//		table.setTableHeader(tableHeader);
		table.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		scrollPane.setViewportView(table);
		
		 ioto_Label = new JLabel("Id of the order:");
		ioto_Label.setForeground(new Color(70, 130, 180));
		ioto_Label.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 21));
		ioto_Label.setBounds(31, 431, 171, 33);
		contentPane.add(ioto_Label);
		
		textField = new JTextField();
		textField.setBounds(216, 431, 125, 33);
		contentPane.add(textField);
		textField.setColumns(10);
		
		btnShip = new JButton("Ship");
		btnShip.setFont(new Font("微软雅黑", Font.PLAIN, 19));
		btnShip.setBounds(368, 431, 94, 33);
		contentPane.add(btnShip);
		btnShip.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Shipping();
			}
		});
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 19));
		btnBack.setBounds(29, 490, 84, 35);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new View_Orders().setVisible(true);
				dispose();
			}
		});
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(419, 14, 98, 32);
		contentPane.add(comboBox);
		
		fillCountries();
	}
	
	public class MyTableModel extends AbstractTableModel {
        /**
         * 表格所有行数据
         */
//        private Object[][] rowData = {
//                {"张三", 80, 80, 80},
//                {"John", 70, 80, 90},
//                {"Sue", 70, 70, 70},
//                {"Jane", 80, 70, 60},
//                {"Joe", 80, 70, 60}
//        };
        
        
//        
//        rowData = new Object[3][3];
		
//		rowData = new Object[20][20];
		
		private int num = 0;
        
        public MyTableModel() {
        	Orders orders = null;
        	
        	for(int i=1;  ; ++i)
        	{
        		try {
					orders = OrdersDao.OrdersSearch(DbUtil.getCon(), i);
					if(orders==null)   break;
					else if(orders.getStatus()==0)
					{
						rowData[num][0] = orders.getId();
						rowData[num][1] = "未发货";
						rowData[num][2] = orders.getCustomer();
						num++;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        	
//        	return rowData;
        }

		/**
         * 返回总行数
         */
        @Override
        public int getRowCount() {
            return num;
        }

        /**
         * 返回总列数
         */
        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        /**
         * 返回列名称（表头名称），AbstractTableModel 中对该方法的实现默认是以
         * 大写字母 A 开始作为列名显示，所以这里需要重写该方法返回我们需要的列名。
         */
        @Override
        public String getColumnName(int column) {
            return columnNames[column].toString();
        }

        /**
         * 返回指定单元格的显示的值
         */
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return rowData[rowIndex][columnIndex];
        }
    }

	protected void Shipping() {
		// TODO Auto-generated method stub
		
		if(!StringUtil.isNum(textField.getText()))
		{
			JOptionPane.showMessageDialog(null, "请输入正确的订单号！");
			return;
		}
		
		if (StringUtil.isEmpty(textField.getText())) {
			JOptionPane.showMessageDialog(null, "ID不能为空！");
			return;
		}
		
		int id = Integer.valueOf(textField.getText());
		
		DbUtil dbUtil = new DbUtil();
		Connection con = null;
		
		try {
			con = dbUtil.getCon();
			Orders orders = OrdersDao.OrdersSearch(con, id);
			
			if(orders==null)
			{
				JOptionPane.showMessageDialog(null, "请输入正确的订单号！");
				return;
			}
			
			if(orders.getStatus()!=0)
				JOptionPane.showMessageDialog(null, "该订单已经发过货了！");
			else
			{
				OrdersDao.OrdersUpdate(con, orders, orders.getId());
				JOptionPane.showMessageDialog(null, "该订单发货成功！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		ioto_Label.setText(rd.getString("ioto_Label"));
		btnShip.setText(rd.getString("btnShip"));
		btnBack.setText(rd.getString("btnBack"));
	}
}
